package com.lz.ccjdemo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Intent跳转工具类
 */
public class IntentUtils {

    public IntentUtils(Builder builder) {
        Intent intent = new Intent(builder.context, builder.clazz);
        if (builder.param.size() > 0) {
            Map<String, Object> param = builder.param;
            for (Map.Entry<String, Object> entry : param.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                if (value instanceof String) {
                    intent.putExtra(key, (String) value);
                } else if (value instanceof Integer) {
                    intent.putExtra(key, (Integer) value);
                } else if (value instanceof Boolean) {
                    intent.putExtra(key, (Boolean) value);
                } else if (value instanceof Float) {
                    intent.putExtra(key, (Float) value);
                } else if (value instanceof Long) {
                    intent.putExtra(key, (Long) value);
                } else {
                    if (value == null) {
                        continue;
                    }
                    if (!(value instanceof Serializable)) {
                        throw new IllegalArgumentException(value.getClass().getName() + " 必须实现Serializable接口!");
                    }
                    intent.putExtra(key, (Serializable) value);
                }
            }
        }
        builder.context.startActivity(intent);
        builder.param.clear();
    }

    public IntentUtils(Builder builder, int tag) {
        Intent intent = new Intent(builder.activity, builder.clazz);
        if (builder.param.size() > 0) {
            Map<String, Object> param = builder.param;
            for (Map.Entry<String, Object> entry : param.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                if (value instanceof String) {
                    intent.putExtra(key, (String) value);
                } else if (value instanceof Integer) {
                    intent.putExtra(key, (Integer) value);
                } else if (value instanceof Boolean) {
                    intent.putExtra(key, (Boolean) value);
                } else if (value instanceof Float) {
                    intent.putExtra(key, (Float) value);
                } else if (value instanceof Long) {
                    intent.putExtra(key, (Long) value);
                } else {
                    if (value == null) {
                        continue;
                    }
                    if (!(value instanceof Serializable)) {
                        throw new IllegalArgumentException(value.getClass().getName() + " 必须实现Serializable接口!");
                    }
                    intent.putExtra(key, (Serializable) value);
                }
            }
        }
        builder.activity.startActivityForResult(intent, tag);
        builder.param.clear();
    }

    public static Builder builder(@NonNull Context context) {
        return new Builder(context);
    }

    public static Builder builder(@NonNull Activity context) {
        return new Builder(context);
    }

    public static class Builder {
        Map<String, Object> param = new HashMap();
        Class<? extends Activity> clazz;
        Context context;
        Activity activity;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder(Activity activity) {
            this.activity = activity;
        }

        public Builder addParam(@NonNull String key, Object value) {
            param.put(key, value);
            return this;
        }

        public IntentUtils start(Class<? extends Activity> clazz) {
            this.clazz = clazz;
            return new IntentUtils(this);
        }


        public IntentUtils startForResult(Class<? extends Activity> clazz, int tag) {
            this.clazz = clazz;
            return new IntentUtils(this, tag);
        }
    }
}
